import Common from '../common'
var Js = Common('shopper')
export default {
	name: 'shopper',
	mixins: [Js],
	components: {},
	data() {
		return {
			fields: [
				{
					key: 'user_no',
					label: 'NO',
					application: [],
					componentName: 'sls-input',
					listType: ''
				},
				{
					key: 'login_name',
					label: '账号',
					application: ['编辑账号', '添加账号'],
					componentName: 'sls-input',
					listType: 'normal',
					placeholder: '请输入账号',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入账号',
						changeFunc: function(e, self) {
							if (e) {
								self.searchItem.login_name = ['LIKE', ['%' + e + '%']];
							} else {
								delete self.searchItem.login_name;
							};
							self.initMainData();
						},
					}
				},
				{
					key: 'password',
					label: '密码',
					application: ['编辑账号', '添加账号'],
					componentName: 'sls-input',
					dialog: {
						type: 'password'
					}
				},
				{
					key: 'mainImg',
					label: '头像',
					application: ['编辑账号', '添加账号'],
					customSlot: 'mainImg',
					componentName: 'upload',
					listType: 'normal',
					limit: 10,
					dialogStyle: 'width:90%',
				},
				{
					key: "name",
					label: '名称',
					application: ['编辑信息', '添加账号'],
					componentName: 'sls-input',
					listType: 'normal',
					formatter: function(val) {
						return val.info.name
					},
					placeholder: '请输入名称',
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入名称',
						changeFunc: function(e, self) {
							if (e) {
								self.UserInfo.searchItem.name = ['LIKE', ['%' + e + '%']]
							} else {
								delete self.UserInfo.searchItem.name
							};
							self.beforeSearch('UserInfo');
						},
					},

				},
				{
					key: "phone",
					label: '电话',
					application: ['编辑信息', '添加账号'],
					componentName: 'sls-input',
					listType: 'normal',
					formatter: function(val) {
						return val.info.phone
					},
					header_search: {
						componentName: 'sls-input',
						style: 'width:160px;margin-right:2px;',
						placeholder: '请输入电话',
						changeFunc: function(e, self) {
							if (e) {
								self.UserInfo.searchItem.phone = ['LIKE', ['%' + e + '%']]
							} else {
								delete self.UserInfo.searchItem.phone
							};
							self.beforeSearch('UserInfo');
						},
					},
				},
				{
					key: 'create_time',
					label: '创建时间',
					listType: 'normal',
					placeholder: '请选择创建时间',
					width: 180,
					header_search: {
						componentName: 'sls-date-time-range',
						style: 'width:160px;margin-right:2px;',
						start_placeholder: '创建开始时间',
						end_placeholder: '创建结束时间',
						changeFunc: function(value, self) {
							if (!value) {
								delete self.searchItem.create_time;
							} else {
								self.searchItem.create_time = ['between', value = value.map(function(e) {
									return e / 1000;
								})]
							};
						},
					},
				},
				{
					label: '操作',
					listType: 'deal',
					width: 200
				},
			],
			// 按钮配置
			btn_info: [

				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑信息'
					},
					func: {
						apiName: function(self) {
							return "api_userInfoUpdate"
						},
						formData: function(self) {
							var data = self.formData.info;
							return data
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.originFormData.info.id,
									user_no: self.originFormData.info.user_no
								},
								data: self.submitData
							}
							if(self.submitData.longitude||self.submitData.latitude){
								postData.saveAfter = [{
									tableName:'User',
									FuncName:'update',
									data:{},
									searchItem:{user_no: self.originFormData.info.user_no}
								}]
								if(self.submitData.longitude){
									postData.saveAfter[0].data.longitude = self.submitData.longitude;
								}
								if(self.submitData.latitude){
									postData.saveAfter[0].data.latitude = self.submitData.latitude;
								}
							}
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'list',
					text: function(data) {
						return '编辑账号'
					},
					func: {
						apiName: function(self) {
							return "api_userUpdate"
						},
						formData: function(self) {

							return self.formData
						},
						postData: function(self) {
							var postData = {
								searchItem: {
									id: self.formData.id,
									user_no: self.formData.user_no,
								},
								data: self.submitData
							}
							return postData;
						}
					},
				},
				{
					type: 'danger',
					icon: 'delete',
					size: 'mini',
					funcType: 'submit',
					position: 'header',
					text: function(data) {
						return '删除选中'
					},
					func: {

						apiName: function(self) {
							return "api_userUpdate"
						},

						postData: function(self) {
							var deleteArray = [];
							for (var i = 0; i < self.selectionArray.length; i++) {
								deleteArray.push(self.selectionArray[i].id);
							};
							var postData = {
								searchItem: {
									id: ['in', deleteArray],
									user_type: 1
								},
								data: {
									status: -1
								}
							};
							return postData;
						}

					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
					text: function(data) {
						return '添加账号'
					},
					func: {
						apiName: function(self) {
							return "api_registerUser"
						},
						formData: function(self) {
							var data = {
								login_name: '',
								password: '',
							};
							return data
						},
						postData: function(self) {
							// var data = self.$$cloneForm(self.submitData);
							// var reg = /^1[3456789]\d{9}$/
							// if (!reg.test(data.phone)) {
							// 	self.$message({message: '手机号格式错误',type: 'warning'});
							// 	return
							// };
							var postData = {
								login_name: self.submitData.login_name,
								password: self.submitData.password,
								role: 12,
								thirdapp_id: 2,
								user_type: 3,
								// needRoleAuth:13
							};
							postData.userInfo = {
								phone: self.submitData.phone,
								name: self.submitData.name,
								user_type: 3,
								behavior: 1
							}
							postData.user = {
								password: self.submitData.password,
								mainImg: self.submitData.mainImg,
								shop_no:self.id,
								user_type: 3,
								behavior: 1
							}
							return postData;
						}
					},
				},
				{
					type: 'info',
					icon: 'edit',
					size: 'mini',
					position: 'header',
				  noAuth:true,
					text: function(data) {
						return '返回'
					},
					funcType: 'func',
					func: {
						func: function(self) {
				      var fullPath = self.$route.fullPath;
				      var tabList = self.$store.state.tabs.tabs;
				      var routerRes = self.$$getBackRoute(tabList,fullPath);
              self.$store.commit('REMOVE_TABS', {
                fullPath:fullPath,
              });
              if(routerRes.backRes){
                self.$router.push(routerRes.backRes);
              }else{
                self.$router.push(routerRes.newRes);
              };
						},
					},
				},
			],

			searchItem: {
				role: 12,
			},
			startSearchItem: {
				role: 12,
			},
			optionData: {
				labelOptions: [],
				behaviorOptions: [],
			},
			otherData: {},
			UserInfo: {
				tableName: 'UserInfo',
				searchItem: {},
				fixSearchItem: {
					status: 1
				},
				key: 'user_no',
				middleKey: 'user_no',
				condition: 'in',
			}

		}

	},

	mounted() {


    this.shop_no = this.$route.params.shop_no;
		this.path = this.$route.params.path;
		this.searchItem.shop_no = this.shop_no;
		this.startSearchItem.shop_no = this.shop_no;
		this.init()
	},
	computed: {
		token: function() {
			return this.$store.getters.getToken
		}
	},
	methods: {

		/**
		 * 初始化
		 */
		init() {
			this.initMainData()
		},

		/**
		 * 列表主函数
		 */
		async initMainData() {

			const self = this;
			const postData = {};
			postData.paginate = self.$$cloneForm(self.paginate);
			postData.token = self.$store.getters.getToken;
			if (self.searchItem) {
				postData.searchItem = self.$$cloneForm(self.searchItem)
			};
			if (JSON.stringify(self.getBefore) != "{}") {
				postData.getBefore = self.$$cloneForm(self.getBefore);
			};
			var res = await self.$$api_userGet({
				data: postData
			});

			self.mainData = res.info.data;
			console.log('self.mainData', self.mainData);
			self.paginate.count = res.info.total;

		},


	},


}
